import 'package:flutter/material.dart';
import 'package:my_app/conponate/user_info.dart';
import '../tools/http_server.dart';
import 'register.dart';

class LoginNameAndPassword extends StatefulWidget {
  const LoginNameAndPassword({Key? key}) : super(key: key);

  @override
  _LoginNameAndPasswordState createState() => _LoginNameAndPasswordState();
}

class _LoginNameAndPasswordState extends State<LoginNameAndPassword> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Colors.blueGrey,
      appBar: AppBar(backgroundColor: Colors.blueGrey,
        title: Text(
          "登录",
        ),
        centerTitle: true,
        elevation: 5.0,
        // 阴影
      ),
      body: FromofLogin(),
    );
  }
}

class FromofLogin extends StatefulWidget {
  const FromofLogin({Key? key}) : super(key: key);

  @override
  _FromofLoginState createState() => _FromofLoginState();
}

class _FromofLoginState extends State<FromofLogin> {
  GlobalKey _globalKey = GlobalKey<FormState>();
  TextEditingController _textEditingController01 = TextEditingController();
  TextEditingController _textEditingController02 = TextEditingController();
  FocusNode _username_focus = FocusNode();
  FocusNode _password_focus = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _textEditingController01.dispose();
    _textEditingController02.dispose();
    _username_focus.dispose();
    _password_focus.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
      child: Form(
        key: _globalKey,
        child: Column(
          children: [
            SizedBox(height: 50),
            TextFormField(
              autofocus: true,
              controller: _textEditingController01,
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.person),
                labelText: "用户名",
                hintText: "username",
              ),
              textInputAction: TextInputAction.next,
            ),
            SizedBox(height: 8),
            TextFormField(
              controller: _textEditingController02,
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.password),
                labelText: "密码",
                hintText: "password",
              ),
              obscureText: true,
              textInputAction: TextInputAction.send,
            ),
            SizedBox(height: 8),
            GestureDetector(
              child: Container(
                width: 420,
                child: Text(
                  "没有账户？请点击此处先注册",
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    color: const Color(0xff414833),
                  ),
                ),
              ),
              onTap: () {
                print("Go to the register page");
                Navigator.of(context).push(MaterialPageRoute(builder: (context) {return RegisterPage();},),);
              },
            ),
            SizedBox(height: 10),
            RaisedButton(
              onPressed: () async {
                print("username:" + _textEditingController01.text);
                print("password:" + _textEditingController02.text);
                String response = await HttpServer.login(
                    _textEditingController01.text,
                    _textEditingController02.text);
                if (response == "登录失败") {
                  showDialog(
                    context: context,
                    builder: (context) {
                      // 用Scaffold返回显示的内容，能跟随主题
                      return Scaffold(
                        backgroundColor: Colors.transparent, // 设置透明背影
                        body: Center(
                          // 居中显示
                          child: Column(
                            // 定义垂直布局
                            mainAxisAlignment: MainAxisAlignment
                                .center, // 主轴居中布局，相关介绍可以搜下flutter-ui的内容
                            children: <Widget>[
                              // CircularProgressIndicator自带loading效果，需要宽高设置可在外加一层sizedbox，设置宽高即可
                              SizedBox(
                                height: 10,
                              ),
                              // 触发关闭窗口
                              RaisedButton(
                                child: Text("返回注册"),
                                onPressed: () {
                                  Navigator.of(context, rootNavigator: true)
                                      .pop();
                                },
                              ),
                            ],
                          ), // 自带loading效果，需要宽高设置可在外加一层sizedbox，设置宽高即可
                        ),
                      );
                    },
                  );
                } else {
                  // 登录成功
                  UserInfo.userName = _textEditingController01.text;
                  Navigator.of(context, rootNavigator: true)
                      .pop(_textEditingController01.text);
                }
              },
              child: Text(
                "登录",
                style: TextStyle(fontSize: 20, color: Colors.white),
              ),
              color: Colors.blueGrey,
            ),
            //   RaisedButton(
            //     onPressed: () {
            //       print("username:" + _textEditingController01.text);
            //       print("password:" + _textEditingController02.text);
            //       HttpServer.signUp(_textEditingController01.text, _textEditingController02.text);
            //     },
            //     child: Text(
            //       "signUp",
            //       style: TextStyle(fontSize: 20, color: Colors.white),
            //     ),
            //     color: Colors.indigo,
            //   ),
          ],
        ),
      ),
    );
  }
}

// 此弹窗用于输入账户与密码不匹配时跳出
class CustomDialogTrue extends StatelessWidget {
  const CustomDialogTrue({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(child: Text('''信息不匹配，请检查是否存在错误输入；
如若未注册，请先注册后在登陆'''));
  }
}

// 此弹窗用于正确登录时弹出
class CustomDialog extends StatelessWidget {
  const CustomDialog({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(child: Text('''登录成功'''));
  }
}